package cn.itsource.basic.controller;

import cn.itsource.basic.util.AjaxResult;
import cn.itsource.basic.util.FastdfsUtil;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/fastDfs")
public class FastDfsController {

    @PostMapping("/upload")
    public AjaxResult upload(@RequestPart("file")MultipartFile file) throws Exception{

        byte[] bytes = file.getBytes();

        String filename = file.getOriginalFilename();
        String extName = filename.substring(filename.lastIndexOf(".") + 1);

        String upload = FastdfsUtil.upload(bytes, extName);
        return AjaxResult.me().setResultObj(upload);
    }

    @DeleteMapping("/delete")
    public AjaxResult delete(@RequestParam("path")String filePath){
        //      /group1/M00/04/6F/CgAIC2QdEdKAD6abAAaOB0C7aiI245.jpg
        String a = filePath.substring(1);   //  group1/M00/04/6F/CgAIC2QdEdKAD6abAAaOB0C7aiI245.jpg
        String group = a.substring(0, a.indexOf("/"));  //  group1
        String path = a.substring(a.indexOf("/") + 1);  //  M00/04/6F/CgAIC2QdEdKAD6abAAaOB0C7aiI245.jpg

        FastdfsUtil.delete(group,path);
        return AjaxResult.me();
    }
}
